//
//  HelpViewController.swift
//  Exchange
//
//  Created by rui on 2018/7/5.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit

class HelpViewController: BaseViewController {

    let reuseIdentifier = "HelpTableViewCell"
    
    @IBOutlet weak var tableView: UITableView!
    
    @IBOutlet weak var searchBtn: UIButton!
    
    @IBOutlet weak var headerView: UIView!
    
    lazy var viewModel: HelpCenterViewModel = {
        let vm = HelpCenterViewModel()
        vm.response = {[weak self](state,isSuccess,errorMsg)in
            if state == ResponseState.mine_help_list {
                if isSuccess {
                    self?.tableView.reloadData()
                }else {
                    if let msg = errorMsg {
                        CYMBShow.showMessage(msg)
                    }
                }
            }
        }
        return vm
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        initUI()
        initData()
    }
    
    func initUI() {
        self.title = kText("帮助中心")
        self.automaticallyAdjustsScrollViewInsets = false
        searchBtn.layer.cornerRadius = 20
        searchBtn.layer.masksToBounds = true
        tableView.rowHeight = 55
        tableView.register(UINib(nibName: reuseIdentifier, bundle: nil), forCellReuseIdentifier: reuseIdentifier)
        tableView.tableFooterView = UIView()
        self.headerView.backgroundColor = kDarkBackgroundColor
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.darkTheme()
    }
    
    func initData() {
        self.viewModel.loadHelpList(id: 5)
    }
    
    @IBAction  func search() {
        let vc = SearchViewController()
        self.navigationController?.pushViewController(vc, animated: true)
    }
}

extension HelpViewController: UITableViewDataSource,UITableViewDelegate {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.viewModel.dataArray.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: reuseIdentifier, for: indexPath) as! HelpTableViewCell
        let model = self.viewModel.dataArray[indexPath.row]
        cell.title.text = model.catName
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: false)
        let model = self.viewModel.dataArray[indexPath.row]
        let vc = HelpDetailViewController()
        vc.model = model
        self.navigationController?.pushViewController(vc, animated: true)
    }
}


